package com.example.blackjack;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.View;

import java.io.OutputStream;
import java.util.Random;
import java.util.Vector;

public class Carts extends View{

	
	Random Generator = new Random();
	
	Vector <Bitmap> v_PlayerCard;
	Vector <Bitmap> v_CropieCard;
	
	Vector <Integer> v_PlayerCardPoint = new Vector<Integer>();
	Vector <Integer> v_CropieCardPoint = new Vector<Integer>();
	
	int CardsBet [] = {
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
		};
		
		Bitmap[] bitmaps = new Bitmap[] {
				
				//spatia
				BitmapFactory.decodeResource(getResources(), R.drawable.as),  
				BitmapFactory.decodeResource(getResources(), R.drawable.twos),
				BitmapFactory.decodeResource(getResources(), R.drawable.thres),
				BitmapFactory.decodeResource(getResources(), R.drawable.fours),
				BitmapFactory.decodeResource(getResources(), R.drawable.fives),
				BitmapFactory.decodeResource(getResources(), R.drawable.sixs),
				BitmapFactory.decodeResource(getResources(), R.drawable.sevens),
				BitmapFactory.decodeResource(getResources(), R.drawable.eits),
				BitmapFactory.decodeResource(getResources(), R.drawable.nines),
				BitmapFactory.decodeResource(getResources(), R.drawable.tens),
				BitmapFactory.decodeResource(getResources(), R.drawable.js),
				BitmapFactory.decodeResource(getResources(), R.drawable.qs),
				BitmapFactory.decodeResource(getResources(), R.drawable.ps),
				
				//pica
				BitmapFactory.decodeResource(getResources(), R.drawable.ap),  
				BitmapFactory.decodeResource(getResources(), R.drawable.twop),
				BitmapFactory.decodeResource(getResources(), R.drawable.threp),
				BitmapFactory.decodeResource(getResources(), R.drawable.fourp),
				BitmapFactory.decodeResource(getResources(), R.drawable.fivep),
				BitmapFactory.decodeResource(getResources(), R.drawable.sixp),
				BitmapFactory.decodeResource(getResources(), R.drawable.sevenp),
				BitmapFactory.decodeResource(getResources(), R.drawable.eitp),
				BitmapFactory.decodeResource(getResources(), R.drawable.ninep),
				BitmapFactory.decodeResource(getResources(), R.drawable.tenp),
				BitmapFactory.decodeResource(getResources(), R.drawable.jp),
				BitmapFactory.decodeResource(getResources(), R.drawable.qp),
				BitmapFactory.decodeResource(getResources(), R.drawable.pp),
				
				//karo
				BitmapFactory.decodeResource(getResources(), R.drawable.ak),  
				BitmapFactory.decodeResource(getResources(), R.drawable.twok),
				BitmapFactory.decodeResource(getResources(), R.drawable.threk),
				BitmapFactory.decodeResource(getResources(), R.drawable.fourk),
				BitmapFactory.decodeResource(getResources(), R.drawable.fivek),
				BitmapFactory.decodeResource(getResources(), R.drawable.sixk),
				BitmapFactory.decodeResource(getResources(), R.drawable.sevenk),
				BitmapFactory.decodeResource(getResources(), R.drawable.eitk),
				BitmapFactory.decodeResource(getResources(), R.drawable.ninek),
				BitmapFactory.decodeResource(getResources(), R.drawable.tenk),
				BitmapFactory.decodeResource(getResources(), R.drawable.jk),
				BitmapFactory.decodeResource(getResources(), R.drawable.qk),
				BitmapFactory.decodeResource(getResources(), R.drawable.pk),
				
				//kupa
				BitmapFactory.decodeResource(getResources(), R.drawable.a1),  
				BitmapFactory.decodeResource(getResources(), R.drawable.two1),
				BitmapFactory.decodeResource(getResources(), R.drawable.thre1),
				BitmapFactory.decodeResource(getResources(), R.drawable.four1),
				BitmapFactory.decodeResource(getResources(), R.drawable.five1),
				BitmapFactory.decodeResource(getResources(), R.drawable.six1),
				BitmapFactory.decodeResource(getResources(), R.drawable.seven1),
				BitmapFactory.decodeResource(getResources(), R.drawable.eit1),
				BitmapFactory.decodeResource(getResources(), R.drawable.nine1),
				BitmapFactory.decodeResource(getResources(), R.drawable.ten1),
				BitmapFactory.decodeResource(getResources(), R.drawable.j1),
				BitmapFactory.decodeResource(getResources(), R.drawable.q1),
				BitmapFactory.decodeResource(getResources(), R.drawable.p1),
				
		
		};

		private Bitmap back_card;

		private int widthCard;
		private int heightCard;

		private int nCardsPlayerCordsX = 0;
		private int nCardsPlayerCordsY = 0;

		private int nCardsCropieCordsX = 0;
		private int nCardsCropieCordsY = 0;

		

		Paint paint = new Paint();

		private int nDrawOffsetCards;

		private int nCartsW;

		private int nCartsH;

		private int nFontSize;

		private Integer nPoint = 0;

		private int nPlayerPointX;

		private int nPlayerPointY;

		private boolean bPlayerLost = false;

		private int nPlayerLostX;

		private int nPlayerLostY;

		private int nDealerPointX;

		private int nDealerPointY;

		private int nDealerPoint = 0;

		private boolean bPlayerWin = false;
		
		
	public Carts(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		widthCard = metrics.widthPixels;
		heightCard = metrics.heightPixels;
		
		if (widthCard == 480){
			nCardsPlayerCordsX = 100;
			nCardsPlayerCordsY = 450;
			nDrawOffsetCards = 35;
			
			nCardsCropieCordsX = 100;
			nCardsCropieCordsY = 70;
			
			nCartsW = 90;
			nCartsH = 115;
			
			nPlayerPointX = 70;
			nPlayerPointY = 540;
			
			nDealerPointX = 70;
			nDealerPointY = 110;
			
			nPlayerLostX = 200;
			nPlayerLostY = 640;
			
			nFontSize = 20;
		}
		
		back_card = BitmapFactory.decodeResource(getResources(), R.drawable.vp_cards_back);

		v_PlayerCard = new Vector<Bitmap>();
		v_CropieCard = new Vector<Bitmap>();
		
		//GetFirstCart();
		
	}
	
	public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
	    int width = bm.getWidth();
	    int height = bm.getHeight();
	    float scaleWidth = ((float) newWidth) / width;
	    float scaleHeight = ((float) newHeight) / height;
	    // CREATE A MATRIX FOR THE MANIPULATION
	    Matrix matrix = new Matrix();
	    // RESIZE THE BIT MAP
	    matrix.postScale(scaleWidth, scaleHeight);

	    // "RECREATE" THE NEW BITMAP
	    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
	    return resizedBitmap;
	}

	
	public void DrawCards(Canvas carts){
		
		paint.setColor(Color.WHITE);
		paint.setStyle(Style.STROKE);
		
		for (int i = 0; i < v_PlayerCard.size(); i++){
			carts.drawBitmap(getResizedBitmap(v_PlayerCard.get(i), nCartsH, nCartsW), nCardsPlayerCordsX+(nDrawOffsetCards*i), nCardsPlayerCordsY, paint);
		}
		
		for (int i = 0; i < v_CropieCard.size(); i++){
			carts.drawBitmap(getResizedBitmap(v_CropieCard.get(i), nCartsH, nCartsW), nCardsCropieCordsX+(nDrawOffsetCards*i), nCardsCropieCordsY, paint);
		}
		
		if (!v_PlayerCardPoint.isEmpty()){
			String Pp = String.valueOf(GetPlayerResult());
			carts.drawText(Pp, nPlayerPointX, nPlayerPointY, paint);
		}
		
		if (!v_CropieCardPoint.isEmpty()){
			String Pd = String.valueOf(GetDealerResult());
			carts.drawText(Pd, nDealerPointX, nDealerPointY, paint);
		}
		
		if (bPlayerLost){
			paint.setColor(Color.RED);
			carts.drawText("You Lost", nPlayerLostX, nPlayerLostY, paint);
		}
		
		if (bPlayerWin){
			paint.setColor(Color.RED);
			carts.drawText("You Win", nPlayerLostX, nPlayerLostY, paint);
		}
		
	}
	
	
	private int GetPlayerResult(){

		paint.setTextSize(nFontSize);
		nPoint = 0;
		for (int i = 0; i < v_PlayerCardPoint.size(); i++){
			if (!v_PlayerCardPoint.isEmpty()){
				nPoint += v_PlayerCardPoint.get(i);
			}
		}

		return nPoint;
	}
	
	private int GetDealerResult(){

		paint.setTextSize(nFontSize);
		nDealerPoint = 0;
		for (int i = 0; i < v_CropieCardPoint.size(); i++){
			if (!v_CropieCardPoint.isEmpty()){
				nDealerPoint += v_CropieCardPoint.get(i);
			}
		}

		return nDealerPoint;
	}
	
	
	public boolean GetYouLost(){
		return bPlayerLost;
	}
	
	public boolean GetPlayerWin(){
		return bPlayerWin;
	}

	
	public void GetFirstCart(){

		for (int i = 0; i < 2; i++){ 
			int GeneratorCardsNum = Generator.nextInt(bitmaps.length);
			v_PlayerCardPoint.add(CardsBet[GeneratorCardsNum]);
			v_PlayerCard.add(bitmaps[GeneratorCardsNum]);
			
		}
		
		for (int j = 0; j < 2; j++){ 
			int GeneratorCropieCards = Generator.nextInt(bitmaps.length);
			if (j == 0){
				v_CropieCardPoint.add(CardsBet[GeneratorCropieCards]);
				v_CropieCard.add(bitmaps[GeneratorCropieCards]);
			}else {
				v_CropieCard.add(back_card);
			}
		}
	}
	
	public void GetHitCarts(){
		
		int GeneratorCardsNum = Generator.nextInt(bitmaps.length);
		v_PlayerCardPoint.add(CardsBet[GeneratorCardsNum]);
		v_PlayerCard.add(bitmaps[GeneratorCardsNum]);

		if (GetPlayerResult() > 21){
			
			for (int i = 0; i < v_PlayerCardPoint.size(); i++){
				if (v_PlayerCardPoint.get(i) == 11){
					v_PlayerCardPoint.set(i, 1);
				}
			}
			
			if (GetPlayerResult() > 21){
				bPlayerLost  = true;
				int GeneratorCropieCards = Generator.nextInt(bitmaps.length);
				v_CropieCardPoint.add(CardsBet[GeneratorCropieCards]);
				v_CropieCard.set(1, bitmaps[GeneratorCropieCards]);
				
			}
			
		}
	}
	
	public void GetStandScenario(){
		
		int GeneratorCropieCards = Generator.nextInt(bitmaps.length);
		v_CropieCardPoint.add(CardsBet[GeneratorCropieCards]);
		v_CropieCard.set(1, bitmaps[GeneratorCropieCards]);

		
		while (GetDealerResult() < 17)
		{
			int GeneratorCropieCards1 = Generator.nextInt(bitmaps.length);
			v_CropieCardPoint.add(CardsBet[GeneratorCropieCards]);
			v_CropieCard.add(bitmaps[GeneratorCropieCards1]);
		}
		
		
		if (GetDealerResult() < GetPlayerResult()){
			bPlayerWin = true;
		}else if (GetDealerResult() > GetPlayerResult()){
			bPlayerLost = true;
		}else if (GetDealerResult() == GetPlayerResult()){

		}
	}
	
	
	
	public void ClearAll(){
		
		nPoint = 0;
		nDealerPoint = 0;
		v_PlayerCardPoint.clear();
		v_PlayerCard.clear();
		
		bPlayerLost = false;
		bPlayerWin = false;
		
		v_CropieCard.clear();
		v_CropieCardPoint.clear();
	}	
}












